package com.sense.kukoo.biz_media.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.feel.common.utils.ToastUtils
import com.sense.kukoo.common.event.SubscriptionChangedEvent
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.lib_logger.LoggerManager
import org.greenrobot.eventbus.EventBus

class MediaControlerViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    private lateinit var mCurrentMusic: LocalMediaListItemBean

    val data_subscribeBtnVisibility:MutableLiveData<Boolean> = MutableLiveData()
    val data_subscribeBtnState:MutableLiveData<Boolean> = MutableLiveData()
    val data_likeBtnVisibility:MutableLiveData<Boolean> = MutableLiveData()
    val data_likeBtnState:MutableLiveData<Boolean> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun setCurrentMusic(music: LocalMediaListItemBean){
        mCurrentMusic = music
    }

    fun loadSubscribeAndLikeState(){
        request {
            //
            if(!Repo.login.isLogin()){
                //未登陆
                data_subscribeBtnVisibility.postValue(true)
                data_subscribeBtnState.postValue(false)
                data_likeBtnVisibility.postValue(true)
                data_likeBtnState.postValue(false)
                return@request
            }
            //
            data_subscribeBtnVisibility.postValue(false)
            data_likeBtnVisibility.postValue(false)
            //
            val isSubscription = Repo.http.isSubscription(mCurrentMusic.idOfBooks())
            if (isSubscription.success){
                val isSub: Boolean = isSubscription.data!!
                data_subscribeBtnVisibility.postValue(true)
                data_subscribeBtnState.postValue(isSub)
            } else {
                Log.e(TAG, "result=" + isSubscription.msg)
                isSubscription.msg.let { sendHintMessage(it) }
            }
            //
            val isLike = Repo.http.isLike(mCurrentMusic.idOfChapter())
            if (isLike.success) {
                val isLikeData: Boolean = isLike.data!!
                //
                data_likeBtnVisibility.postValue(true)
                data_likeBtnState.postValue(isLikeData)
            } else {
                Log.e(TAG, "result=" + isLike.msg)
                isLike.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun switchSubscription(){
        request {
            val result = Repo.http.subscription(mCurrentMusic.idOfBooks())
            if (result.success){
                val isSubscription:Boolean = !(data_subscribeBtnState.value == true)
                data_subscribeBtnState.postValue(isSubscription)
                //通知详情页
                EventBus.getDefault().post(
                        SubscriptionChangedEvent(mCurrentMusic.idOfBooks(),
                                                 isSubscription,
                                                 SubscriptionChangedEvent.FROM_CONTROLER)
                )
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { ToastUtils.showToast(it) }
            }
        }
    }

    fun switchLikeState(){
        request {
            val result = Repo.http.like(mCurrentMusic.idOfChapter(), mCurrentMusic.idOfBooks())
            if (result.success){
                val like:Boolean = !(data_likeBtnState.value == true)
                data_likeBtnState.postValue(like)
                LoggerManager.e("result.success, like=${like}")
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { ToastUtils.showToast(it) }
            }
        }
    }
}